package service.servlet;

import dao.Userdao;
import dao.imp.Userdaoimp;
import domain.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/*servlet 是完成业务逻辑
*
*
* */
@WebServlet("/demo1")
public class loginservlet extends HttpServlet {
    Userdao userdao=new Userdaoimp();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        User loginUser=new User(username,password);
        System.out.println(username);
        System.out.println(password);
        try {
            List<User> users=userdao.selectUser(loginUser);
            /*集合里面有数据说明用户名和密码正确 跳转到Success
            * 否则跳转到FailServlet
            * 根据现有业务 如果找到 list集合里只有一个数据
            * */
            if (users.size()>0){
                User user =users.get(0);
                /*需要将user存储到request域 请求转发的数据就是request域当中 并且是一次请求*/
                req.setAttribute("u",user);
                /*开始跳转到SuccessServlet*/
                req.getRequestDispatcher("/success").forward(req,resp);
            }else{
                req.getRequestDispatcher("/fail").forward(req,resp);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
